﻿@model Manager.Models.MaterialClassViewModel

@{
	ViewBag.Title = "Create";
	Layout = "~/Views/Shared/_Layout.cshtml";
	var Selects = Model.Selects;
	var type = Model.Item.GetType();
}

<h3>Create <b>@Model.TypeName</b></h3>

@using (Html.BeginForm())
{
	@Html.AntiForgeryToken()

	@*@Helpers.GenerateEditor(type, Selects)*@
	<div class="form-horizontal">
		<hr />
		@Html.ValidationSummary(true, "", new { @class = "text-danger" })
		@Html.Hidden("typeName", Model.TypeName)

		@foreach (var prop in type.GetProperties())
		{
			var val = type.GetProperty(prop.Name).GetValue(Model.Item);

			if (Selects.ContainsKey(prop.PropertyType.Name))
			{
				<div class="form-group">
					@Html.Label(prop.Name, htmlAttributes: new { @class = "control-label col-md-3" })
					<div class="col-md-9 dropdown">
						@Html.DropDownList(prop.Name, Selects[prop.PropertyType.Name], "", new { @class = "form-control" })
					</div>
				</div>
			}
			else if (prop.Name.ToUpper() != "ID")
			{
				if (prop.PropertyType.Namespace.Contains("System"))
				{
					var inputType = "text";
					var step = "";
					switch (prop.PropertyType.Name)
					{
						case "Int32":
							inputType = "number";
							break;
						case "Single":
							inputType = "number";
							step = "step=any";
							break;
						case "Nullable`1":
							inputType = "date";
							break;
					}
					<div class="form-group">
						@*@if (prop.CustomAttributes.Where(a => a.AttributeType.Name == "NotMappedAttribute").Count() > 0)
							{
								string desc = "";
								var res = prop.CustomAttributes.Where(a => a.AttributeType.Name == "DisplayAttribute").FirstOrDefault();
								if (res != null && res.NamedArguments.Count > 0)
								{
									desc = res.NamedArguments[0].TypedValue.Value.ToString();
									var spl = desc.Split('=');
									<a href="@desc" target="_blank" class="control-label col-md-3" title="@((spl.Length > 1)?spl[1]: "Not mapped")">@prop.Name</a>
								}
								else
								{
									@Html.Label(prop.Name, htmlAttributes: new { @class = "control-label col-md-3" })
								}
							}
							else
							{*@
						@Html.Label(prop.Name, htmlAttributes: new { @class = "control-label col-md-3" })
						@*}  *@
						<div class="col-md-9">
							<input class="form-control" type="@inputType" @step name="@prop.Name" value="@val" />
						</div>
					</div>
				}
				else if (prop.PropertyType.Name == "UnitType")
				{
					<div class="form-group">
						@Html.Hidden(prop.Name, val, new { @id = prop.Name })
						@Html.Label(prop.Name, htmlAttributes: new { @class = "control-label col-md-3" })
						<div class="col-md-9">
							<a class="btn btn-info" title="Select">@val</a>
						</div>
					</div>
				}
				else
				{
					<div class="form-group">
						@Html.Label(prop.Name, htmlAttributes: new { @class = "control-label col-md-3" })
						<div class="col-md-9">
							<input class="form-control" name="@prop.Name" value="@val" />
						</div>
					</div>
				}
			}
		}

		<div class="form-group">
			<div class="col-md-offset-2 col-md-9">
				<input type="submit" value="Create" class="btn btn-default" />
			</div>
		</div>
	</div>
}

<div>
	@Html.ActionLink("Back to List", "Index", new { n = Model.TypeName })
</div>
